

//https://leetcode.cn/problems/subtree-of-another-tree/
//判断一棵树是不是另外一棵树的子树
public class SubRoot {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(subRoot == null) return true;
        if(root == null) return false;  //能走到这里说明subRoot不为空
        return isSameTree(root,subRoot) || isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot);
    }

    private boolean isSameTree(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return true;
        }
        if (root1 == null || root2 == null) {
            return false;
        }
        return root1.val == root2.val
                && isSameTree(root1.left, root2.left)
                && isSameTree(root1.right, root2.right);
    }
}
